package in.co.codedoc.sql;

import java.io.IOException;
import java.io.PrintWriter;

public class AliasedTableColumn
	implements TableColumn
{
	public AliasedTableColumn(String alias,TableColumn wrappedColumn)
	{
		this.alias = alias;
		this.wrappedColumn = wrappedColumn;
		this.table = new Table(wrappedColumn.GetTable(),alias);
	}
	
	@Override
	public PrintWriter Generate(PrintWriter out)
		throws IOException
	{
		out.print(GetQualifiedName());
		return out;
	}

	@Override
	public DBOutputColumnMap GetDBOutputColumnMap()
	{
		return wrappedColumn.GetDBOutputColumnMap();
	}

	@Override
	public Table GetTable()
	{
		return table;
	}
	
	@Override
	public String GetName()
	{
		return wrappedColumn.GetName();
	}

	@Override
	public String GetQualifiedName()
	{
		return alias + "." + GetName();
	}

	@Override
	public boolean equals(Object t)
	{
		if(t == null || !(t instanceof AliasedTableColumn))
		{
			return false;
		}

		AliasedTableColumn that = (AliasedTableColumn)t;

		return alias != null && alias.equals(that.alias) && wrappedColumn.equals(that.wrappedColumn);
	}

	@Override
	public int hashCode()
	{
		return alias.hashCode() ^ wrappedColumn.hashCode();
	}
	

	private Table table;
	private TableColumn wrappedColumn;
	private String alias;
}
